
local bb = require("badboy")
local pos = bb.loadpos()


function tap(x,y)
	if x > -1 and y > -1 then
		touchDown(1, x, y)
		mSleep(10)
		touchUp(1, x, y) 
	end
end

function string.split(str, delimiter)
	if str==nil or str=='' or delimiter==nil then
		return nil
	end
	
	local result = {}
	for match in (str..delimiter):gmatch("(.-)"..delimiter) do
		table.insert(result, match)
	end
	return result
end

function initGameData()
	model,practice,maoxian,fuhuo = 0;
	herotype1,herotype2,herotype3 = 0;
	heroseq1,heroseq2,heroseq3 = 0;
	tiyanka = 0;
	maoxian="1";
	for v, s in pairs(results) do
		if v == "RadioGroup1" then
			model = s;
		elseif v == "RadioGroup2" then
			practice = s;
		elseif v == "RadioGroup4" then
			fuhuo = s;
		elseif v == "RadioGroup5" then
			tiyanka = s;
		elseif v == "ComboBox1_1" then
			herotype1 = s;
		elseif v == "ComboBox2_1" then
			herotype2 = s;
		elseif v == "ComboBox3_1" then
			herotype3 = s;
		elseif v == "CheckBoxGroup1_2" then
			heroseq1 = s;
		elseif v == "CheckBoxGroup2_2" then
			heroseq2 = s;
		elseif v == "CheckBoxGroup3_2" then
			heroseq3 = s;
		end
	end
end

--购买装备和学技能
function buyAndLearn()
	jineng = findUIByName("学技能");
	if jineng then
		x,y = findPointByName("学技能");
		tap(x,y);
	end
	dajineng = findUIByName("学大技能");
	if dajineng then
		x,y = findPointByName("学大技能");
		tap(x,y);
	end
	
	zhuangbei = findUIByName("购买装备");
	if zhuangbei then
		x,y = findPointByName("购买装备");
		tap(x+30,y+30);
	end
	
	local x, y = findPointByName("游戏中装备展开")
	--tap(x,y)
end

function attackEnemy()
	sysLog("attackEnemy")
	tap(1154,602);
	tap(956,640);
	tap(1034,498);
	tap(1167,414);
	tap(744,645);
end

function move2enemy()
	
	
	
	local my_x,my_y = findPointByName("小地图我方英雄坐标")
	if my_x < 0 then
		my_x = 22
		my_y = 202;
	end
	
	--p1:click(100,500)
	--move:touchMoveTo(p3,20,100,500);
	point = findColors({0, 0, 1279, 719}, 
		"0|0|0xf41a19,1|0|0xf41a19,2|1|0xf21918",
		95, 0, 0, 0)
	mypos = pos:new(my_x, my_y)
	local lastPos = 1000;
	local miniPos = pos:new(1000,1000);
	
	if #point ~= 0 then
		for v,s in ipairs(point) do
			p1 = pos:new(s["x"],s["y"]);
			value = p1:distanceBetween(mypos);
			if value < lastPos then
				lastPos = value;
				miniPos = p1;
				sysLogFmt('lastPos:%d',lastPos)
			end
		end
	end
	
	touchDown(1, 161, 564)
	mSleep(200)
	movex = miniPos.x - my_x;
	movey = miniPos.y-my_y;
	sysLogFmt('[%d,%d]',movex,movey)
	touchMove(1,161+movex,564+movey);
	mSleep(1000)
	touchUp(1,161,564)
	
end


function findUIByName(name)
	currentUI = uis[name]
	for key, value in pairs(currentUI) do
		x, y = findColor({value[1], value[2], value[3], value[4]}, value[5],value[6], value[7], value[8], value[9])
		if x > -1 then
			sysLogFmt('寻找%s成功',name)
			return true;
		end
	end	
	--如果什么都找不到，就返回主线任务
	
	return false;
end

function findPointByName(name)
	currentUI = uis[name]
	for key, value in pairs(currentUI) do
		x, y = findColor({value[1], value[2], value[3], value[4]}, value[5],value[6], value[7], value[8], value[9])
		return x,y;
	end	
end

